package cm.com.blackboard.DataModule4B2.customizeConfigUtils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class BaseXMLUtil {
	
	public static List parseXML(String profilepath, Class clazz) throws JDOMException, IOException, InstantiationException, IllegalAccessException{
		List list = new ArrayList<Object>();
    	String docStr = URLDecoder.decode(profilepath, "utf-8");
    	SAXBuilder builder = new SAXBuilder(false);
    	Document doc = builder.build(docStr);
    	Element root = doc.getRootElement();
    	List modelList = root.getChildren(clazz.getSimpleName());
    	for(Iterator iter = modelList.iterator(); iter.hasNext();){   	
    		Object model = clazz.newInstance();
    		Element modelEle = (Element) iter.next();
    		try {    
    	        Field[] fields = clazz.getDeclaredFields(); 
    	        for (Field field : fields) {  
    	            PropertyDescriptor pd = new PropertyDescriptor(field.getName(),  
    	                    clazz);  
    	            Method setMethod = pd.getWriteMethod();
    	            setMethod.invoke(model, modelEle.getChildTextTrim(field.getName()));
    	        }  
    	    } catch (SecurityException e) {  
    	        e.printStackTrace();  
    	    } catch (IllegalArgumentException e) {  
    	        e.printStackTrace();  
    	    } catch (IntrospectionException e) {  
    	        e.printStackTrace();  
    	    } catch (IllegalAccessException e) {  
    	        e.printStackTrace();  
    	    } catch (InvocationTargetException e) {  
    	        e.printStackTrace();  
    	    }
    		list.add(model);
    	}
    	return list;
    }
	public static void renderXML(String profilepath, Class clazz, List list) throws FileNotFoundException, IOException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
    	Document doc = new Document();
    	Element root = new Element("Models");
    	doc.addContent(root);
    	for(Object obj:list){
    		Field[] fields = clazz.getDeclaredFields(); 
    		Element model = new Element(clazz.getSimpleName());
	        for (Field field : fields) { 
	        	PropertyDescriptor pd = new PropertyDescriptor(field.getName(),  
	                    clazz);  
	        	Method getMethod = pd.getReadMethod();
	        	Element subAttr = new Element(field.getName());
	        	subAttr.setText((String) getMethod.invoke(obj));		
	        	model.addContent(subAttr);
	        }   		
	        root.addContent(model); 		
    	}
    	XMLOutputter out = new XMLOutputter();
    	Format format = Format.getPrettyFormat();
    	format.setIndent("   ");
    	format.setEncoding("utf-8");
    	out.setFormat(format);
    	out.output(doc,new FileOutputStream(profilepath));	
    }
}
